package com.example.administrator.adroidtest;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import static com.example.administrator.adroidtest.ResultCode.RESUTLT_OK;

public class MeasurementParamListActivity extends Activity implements AdapterView.OnItemClickListener {

    private ListView lv;
    private Button newMeasureDefiniton_bt;
    public MeasurementParamListAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement_param_list);
        initpage();
    }

    /**
     * 初始化页面
     */
    public void initpage(){
        //init lv
        lv=(ListView) findViewById(R.id.MeasureDefinition_list);
        lv.setOnItemClickListener(this);//设置监听器
        //init adapter
        adapter=new MeasurementParamListAdapter(this);
        //set adapter
        lv.setAdapter(adapter);

        newMeasureDefiniton_bt=(Button) findViewById(R.id.newMeasureDefiniton_bt);
    }


    /**
     * 点击新建按钮时的操作
     *
     */
    public void onMeasureDefinition(View view) {
        /**
         * 弹出框输入文字
         */
        //new AlertDialog.Builder(this).setTitle("请输入").setView(new EditText(this)).setPositiveButton("确定",null).setNegativeButton("取消",null).show();

        Intent intent = new Intent();
        intent.setClass(this, MeasurementParamAddActivity.class);
        startActivityForResult(intent,1);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


    }

    public void ontext_bt(View view) {
//        Intent intent=new Intent(this,UserTestctivity.class);
//        startActivityForResult(intent,0);


        MeasurementParamManager mgr = MeasurementParamManager.getInstance();

        System.out.println(mgr.getMeasurementparamslist().get(0).getMdfArray()[0].getWaveformType());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESUTLT_OK:
       //         Bundle bundle = data.getExtras();
                /*
                User user=(User)bundle.getSerializable("user");
                Toast.makeText(getApplicationContext(),user.getName(),Toast.LENGTH_SHORT).show();
                */
                adapter.arr.add("");
                adapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }


    /**
     * ListView的adapter
     */
    public class MeasurementParamListAdapter extends BaseAdapter {
        private Context context;
        private LayoutInflater inflater;
        public ArrayList<String> arr;
        public  MeasurementParamListAdapter(Context context){
            super();
            this.context=context;
            inflater=LayoutInflater.from(context);
            arr=new ArrayList<String>();
            for(int i=0;i<3;i++){
                arr.add(" ");
            }
        }
        @Override
        public int getCount() {
            return arr.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null){
                view=inflater.inflate(R.layout.mlistview_item,null);
            }
            final EditText edt=(EditText) view.findViewById(R.id.mlistview_item_edittext);
            edt.setText(arr.get(i));
            Button del=(Button) view.findViewById(R.id.mlistview_button);
            edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(arr.size()>0){
                        arr.set(i,edt.getText().toString());
                    }
                }
            });
            del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    arr.remove(i);
                    adapter.notifyDataSetChanged();

                }
            });
            return view;
        }
    }
}
